<template>
  <div class="pay_config">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="是否启用" prop="switchConfig">
        <el-select v-model="dataForm.switchConfig" placeholder="请选择是否启用">
          <el-option label="关闭" value="0"></el-option>
          <el-option label="启用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="绑定" prop="payTypeId">
        <el-select v-model="dataForm.payTypeId" placeholder="请选择支付方式">
          <el-option v-for="item in payTypes" v-if="item.isDel != 1" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商户号" prop="sellerMerchantNo"><el-input v-model="dataForm.sellerMerchantNo" placeholder="商户号"></el-input></el-form-item>
      <el-form-item label="终端号" prop="sellerTerminalId"><el-input v-model="dataForm.sellerTerminalId" placeholder="终端号"></el-input></el-form-item>
      <el-form-item label="TOKEN 令牌" prop="sellerToken"><el-input v-model="dataForm.sellerToken" placeholder="令牌"></el-input></el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer"><el-button type="primary" @click="dataFormSubmit()">确定</el-button></span>
    <!-- <el-form :v-model="testPay" ref="testPay" @keyup.enter.native="testInterface()">
      <el-form-item label="用户条形码"><el-input v-model="testPay.auth_no" placeholder="条形码"></el-input></el-form-item>
      <div><el-button type="error" @click="testInterface()">测试</el-button></div>
    </el-form> -->
  </div>
</template>

<script>
// import CryptoJS from 'crypto-js';
export default {
  data() {
    return {
      payTypes: [],
      dataForm: {
        id: 0,
        sellerId: '',
        switchConfig: '',
        payTypeId: '',
        sellerMerchantNo: '',
        sellerTerminalId: '',
        sellerToken: ''
      },
      testPay: {
        auth_no: ''
      },
      dataRule: {
        sellerId: [{ required: true, message: '对应酒店ID不能为空', trigger: 'blur' }],
        switchConfig: [{ required: true, message: '是否启用 0,关闭 1,启用不能为空', trigger: 'blur' }],
        payTypeId: [{ required: true, message: '绑定支付方式ID不能为空', trigger: 'blur' }],
        sellerMerchantNo: [{ required: true, message: '商户号不能为空', trigger: 'blur' }],
        sellerTerminalId: [{ required: true, message: '终端号不能为空', trigger: 'blur' }],
        sellerToken: [{ required: true, message: '令牌不能为空', trigger: 'blur' }]
      }
    };
  },
  methods: {
    init() {
      this.$http({
        url: this.$http.adornUrl(`/hotel/thotelsellerpayinterfaceconfig/myInfo`),
        method: 'get'
      }).then(({ data }) => {
        console.log(data);
        if (data && data.code === 0) {
          this.dataForm.id = data.tHotelSellerPayInterfaceConfig.id;
          this.dataForm.sellerId = data.tHotelSellerPayInterfaceConfig.sellerId;
          this.dataForm.switchConfig = data.tHotelSellerPayInterfaceConfig.switchConfig != null ? data.tHotelSellerPayInterfaceConfig.switchConfig + '' : '';
          this.dataForm.payTypeId = data.tHotelSellerPayInterfaceConfig.payTypeId;
          this.dataForm.sellerMerchantNo = data.tHotelSellerPayInterfaceConfig.sellerMerchantNo;
          this.dataForm.sellerTerminalId = data.tHotelSellerPayInterfaceConfig.sellerTerminalId;
          this.dataForm.sellerToken = data.tHotelSellerPayInterfaceConfig.sellerToken;
        }
        console.log(this.dataForm.id);
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        console.log(this.dataForm);
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/hotel/thotelsellerpayinterfaceconfig/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              sellerId: this.dataForm.sellerId,
              switchConfig: this.dataForm.switchConfig,
              payTypeId: this.dataForm.payTypeId,
              sellerMerchantNo: this.dataForm.sellerMerchantNo,
              sellerTerminalId: this.dataForm.sellerTerminalId,
              sellerToken: this.dataForm.sellerToken
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.init();
                  this.RemoveCheck();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    loadPayType() {
      //加载支付方式
      this.$http({
        url: this.$http.adornUrl('/hotel/thotelfloorroompaytype/list'),
        method: 'get',
        params: this.$http.adornParams({
          page: 1,
          limit: 999,
		  state: 1
        })
      }).then(({ data }) => {
        if (data.code === 0) {
          this.payTypes = data.page.list;
        } else {
          this.$message.error('未知错误，请检查数据，或刷新后重试');
        }
      });
    },
    testInterface() {
      let trace = this.rundomTrace();
      let time = this.GetDateTimeToString();
      let total_fee = '1';
      this.$http({
        url: this.$http.adornUrl('/hotel/thotelsellerpayinterfaceconfig/preAuthBar'),
        method: 'post',
        data: this.$http.adornData({
          type: 1, //测试
          merchant_no: this.dataForm.sellerMerchantNo, //商户号
          terminal_id: this.dataForm.sellerTerminalId, //终端号
          auth_no: this.testPay.auth_no, //授权码，客户的付款码
          total_fee: total_fee, //金额，单位分
          order_body: '测试', //订单描述
          attach: '测试', //附加数据,原样返回
          key_sign: '' //签名字符串,拼装所有非空参数+令牌，UTF-8编码，32位md5加密转换
        })
      }).then(data => {
        console.log(data);
      });
    },
    RemoveCheck() {
      this.$refs.dataForm.clearValidate(); //移除该表单项的校验结果
    },
    rundomTrace() {
      var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
      var maxPos = $chars.length;
      var pwd = '';
      for (let i = 0; i < 32; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return pwd;
    },
    GetDateTimeToString() {
      var date_ = new Date();
      var year = date_.getFullYear();
      var month = date_.getMonth() + 1;
      var day = date_.getDate();
      if (month < 10) month = '0' + month;
      if (day < 10) day = '0' + day;

      var hours = date_.getHours();
      var mins = date_.getMinutes();
      var secs = date_.getSeconds();
      var msecs = date_.getMilliseconds();
      if (hours < 10) hours = '0' + hours;
      if (mins < 10) mins = '0' + mins;
      if (secs < 10) secs = '0' + secs;
      if (msecs < 10) secs = '0' + msecs;
      return year + month + day + hours + mins + secs;
    }
  },
  mounted() {
    console.log('准备初始化');
    this.init();
    this.loadPayType();
  }
};
</script>

<style lang="scss" scoped>
.pay_config {
  .el-input {
    width: 30%;
  }
}
</style>
